#! /bin/bash
# -*- tab-width: 4; -*-
# vi: set sw=2 ts=4:

# Regenerate the reference ktx2 files for load tests, etc.

# Copyright 2017 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0

# Depth of this script relative to the project root
depth=../..

# Change dir to the testimages folder, the script location...
cd $(dirname $0)

# Make paths relative to the testimages directory.
ktx_root=$depth
toktx_vs2013=$ktx_root/build/msvs/win/vs2013/x64/Release/toktx.exe
toktx_vs2015=$ktx_root/build/msvs/win/vs2015/x64/Release/toktx.exe
toktx_cmake=$ktx_root/build/cmake/linux/Release/toktx
toktx_cmake_d=$ktx_root/build/cmake/linux/Debug/toktx
toktx_make=$ktx_root/build/make/linux/out/Release/toktx
toktx_make_d=$ktx_root/build/make/linux/out/Debug/toktx

ktx2ktx2_vs2013=$ktx_root/build/msvs/win/vs2013/x64/Release/ktx2ktx2.exe
ktx2ktx2_vs2015=$ktx_root/build/msvs/win/vs2015/x64/Release/ktx2ktx2.exe
ktx2ktx2_cmake=$ktx_root/build/cmake/linux/Release/ktx2ktx2
ktx2ktx2_cmake_d=$ktx_root/build/cmake/linux/Debug/ktx2ktx2
ktx2ktx2_make=$ktx_root/build/make/linux/out/Release/ktx2ktx2
ktx2ktx2_make_d=$ktx_root/build/make/linux/out/Debug/ktx2ktx2

# Ensure generation is not polluted by user environment
unset TOKTX_OPTIONS

if [ -n "$1" -a -x "$1" ]; then
  toktx="$1"
elif [ -x "$toktx_vs2013" ]; then
  toktx=$toktx_vs2013
elif [ -x "$toktx_vs2015" ]; then
  toktx=$toktx_vs2015
elif [ -x "$toktx_cmake" ]; then
  toktx=$toktx_cmake
elif [ -x "$toktx_cmake_d" ]; then
  toktx=$toktx_cmake_d
elif [ -x "$toktx_make" ]; then
  toktx=$toktx_gmake
elif [ -x "$toktx_make_d" ]; then
  toktx=$toktx_gmake
elif which toktx >/dev/null; then
  toktx=toktx
else
  echo $0: None of $toktx_vs2013, $toktx_vs2015, $toktx_gmake or $toktx_cmake found.
  echo $0: Nor was toktx found in along PATH.
  echo $0: Aborting generation
  exit 1
fi

if [ -n "$2" -a -x "$2" ]; then
  ktx2ktx2="$1"
elif [ -x "$ktx2ktx2_vs2013" ]; then
  ktx2ktx2=$ktx2ktx2_vs2013
elif [ -x "$ktx2ktx2_vs2015" ]; then
  ktx2ktx2=$ktx2ktx2_vs2015
elif [ -x "$ktx2ktx2_cmake" ]; then
  ktx2ktx2=$ktx2ktx2_cmake
elif [ -x "$ktx2ktx2_cmake_d" ]; then
  ktx2ktx2=$ktx2ktx2_cmake_d
elif [ -x "$ktx2ktx2_make" ]; then
  ktx2ktx2=$ktx2ktx2_gmake
elif [ -x "$ktx2ktx2_make_d" ]; then
  ktx2ktx2=$ktx2ktx2_gmake
elif which ktx2ktx2 >/dev/null; then
  ktx2ktx2=ktx2ktx2
else
  echo $0: None of $ktx2ktx2_vs2013, $ktx2ktx2_vs2015, $ktx2ktx2_gmake or $ktx2ktx2_cmake found.
  echo $0: Nor was ktx2ktx2 found in along PATH.
  echo $0: Aborting generation
  exit 1
fi

# Generate reference files for toktx-tests ...

$ktx2ktx2 --test -f -o orient-down-metadata-u.ktx2 orient-down-metadata.ktx
$ktx2ktx2 --test -f -o orient-up-metadata-u.ktx2 orient-up-metadata.ktx
$ktx2ktx2 --test -f pattern_02_bc2.ktx
$ktx2ktx2 --test -f -o rgb-mipmap-reference-u.ktx2 rgb-mipmap-reference.ktx
$ktx2ktx2 --test -f -o rgba-reference-u.ktx2 rgba-reference.ktx
$ktx2ktx2 --test -f texturearray_astc_8x8_unorm.ktx
$ktx2ktx2 --test -f texturearray_bc3_unorm.ktx
$ktx2ktx2 --test -f texturearray_etc2_unorm.ktx
$toktx --test --t2 --mipmap rgb-mipmap-reference-u.ktx2 ../srcimages/level0.ppm ../srcimages/level1.ppm ../srcimages/level2.ppm ../srcimages/level3.ppm ../srcimages/level4.ppm ../srcimages/level5.ppm ../srcimages/level6.ppm
$toktx --test --bcmp FlightHelmet_baseColor_basis.ktx2  ../srcimages/FlightHelmet_baseColor.png
$toktx --test --bcmp alpha_simple_basis.ktx2 ../srcimages/alpha_simple.png
$toktx --test --bcmp kodim17_basis.ktx2 ../srcimages/kodim17.png
$toktx --test --bcmp color_grid_basis.ktx2 ../srcimages/color_grid.png

# ... and additional files for the load tests.

$toktx --test --lower_left_maps_to_s0t0 rgb-reference-metadata.ktx ../srcimages/rgb.ppm
$toktx --test --genmipmap --threads 1 --zcmp 5 --uastc --uastc_rdo_q 3 Iron_Bars_001_normal_uastc_rdo3_zstd5.ktx2 ../srcimages/Iron_Bars/Iron_Bars_001_normal.jpg

# ... these too but these commands will only work with my arrangement
# of workareas, so keep commented out.
$toktx --test --genmipmap --bcmp -scale 0.5 ../webgl/libktx-webgl/ktx_app_basis.ktx2 ../../../KTX-Specification/icons/png/ktx_app.png
$toktx --test --uastc --uastc_rdo_q 5.0 --resize 1000x1392 ../webgl/libktx-webgl/ktx_document_uastc_rdo5.ktx2 ../../../KTX-Specification/icons/png/ktx_document.png
$toktx --test --genmipmap -bcmp ktx_document_basis.ktx2 ../../../KTX-Specification/icons/png/ktx_document.png
# threads 1 here is to avoid non-determism in the RDO processing.
# Hopefully a fix will be forthcoming.
$toktx --test --genmipmap --threads 1 --uastc --uastc_rdo_q 4 --zcmp 5 ktx_document_uastc_rdo4_zstd5.ktx2 ../../../KTX-Specification/icons/png/ktx_document.png

$toktx --test --cubemap --genmipmap --bcmp cubemap_yokohama_basis_rd.ktx2 @@../srcimages/Yokohama3/filelist.txt

# ... this also but it takes a long time to generate especially with
# the debug version.
$toktx --test --cubemap -scale 0.5 --genmipmap --threads 1 --uastc --uastc_rdo_q 4 --zcmp 5 cubemap_goldengate_uastc_rdo4_zstd5_rd.ktx2 @@../srcimages/GoldenGateBridge3/filelist.txt
